package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.QuestionComment;
import cn.wolfcode.trip.base.domain.QuestionCommentReply;
import cn.wolfcode.trip.base.mapper.QuestionCommentMapper;
import cn.wolfcode.trip.base.mapper.QuestionCommentReplyMapper;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.QuestionCommentQueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionCommentMapper commentMapper;
    @Autowired
    private QuestionCommentReplyMapper commentReplyMapper;

    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Question> list = questionMapper.queryForList(qo);
        return new PageInfo(list);
    }

    public void save(Question question) {
        question.setCreateTime(new Date());
        questionMapper.insert(question);
    }

    public Question getById(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    public PageInfo getCommentsByQuestionId(QuestionCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<QuestionComment> list = commentMapper.getCommentsByQuestionId(qo.getQuestionId());
        return new PageInfo(list);
    }

    public void save(QuestionComment questionComment) {
        //设置创建时间
        questionComment.setCreateTime(new Date());
        commentMapper.insert(questionComment);
    }

    public void updateNum(Question question) {
        questionMapper.updatePageviews(question);
    }

    public void saveReply(QuestionCommentReply commentReply) {
        commentReply.setCreateTime(new Date());
        commentReplyMapper.insert(commentReply);
    }


}
